<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:rich="http://richfaces.org/rich"
  xmlns:a4j="http://richfaces.org/a4j"
  xmlns:f="http://java.sun.com/jsf/core"
  xmlns:zanata="http://java.sun.com/jsf/composite/zanata"
  template="../WEB-INF/template/template.xhtml">

  <f:metadata>
    <f:event type="preRenderView" listener="#{identity.checkRole('admin')}"/>
  </f:metadata>

  <ui:define name="page_title">#{msgs['jsf.ManageSearch']}</ui:define>
  <ui:define name="center_content">

    <a4j:jsFunction name="clearAllSelection" action="#{reindexAction.setSelectAll(false)}"
      render="actions" limitRender="true"/>

    <a4j:jsFunction name="executeSelection" action="#{reindexAction.reindexDatabase}"
      render="actions,progress" limitRender="true"/>

    <div class="g">
      <div class="g__item w--1-m w--3-8-l w--1-3 l--push-bottom-half">
        <p class="txt--meta l--push-all-0">
          <a href="/admin/home">
            #{msgs['jsf.Administration']}
          </a>
        </p>

        <div class="l--push-bottom-half">
          <h1 class="l--push-all-0">
            <i class="i--small i--search txt--neutral i--left"></i>
            #{msgs['jsf.ManageSearch']}
          </h1>
        </div>

        <h:panelGroup layout="block"  styleClass="panel l--push-top-2" id="progress">
          <div class="panel__header">
            <h:panelGroup layout="block"  styleClass="panel__header__actions l--push-right-half l--push-bottom-0"
              rendered="#{reindexAction.inProgress and !reindexAction.canceled}">
              <h:form styleClass="l--push-bottom-0">
                <a4j:commandLink id="cancel" styleClass="button--small button--link"
                  value="#{msgs['jsf.ManageSearch.Abort']}"
                  action="#{reindexAction.cancel}" render="actions,progress"/>
              </h:form>
            </h:panelGroup>
            <h2 class="panel__heading--small">#{msgs['jsf.ManageSearch.CurrentProgress']}</h2>
          </div>

          <div class="l--pad-all-half">
            <ui:fragment rendered="#{!reindexAction.inProgress}">
              <p id="noOperationsRunning" class="txt--meta">
                <h:outputText escape="false"
                  value="#{msgs['jsf.ManageSearch.NoOperationsRunning']}" />
              </p>
            </ui:fragment>
            <ui:fragment rendered="#{reindexAction.reindexedSinceServerRestart and !reindexAction.inProgress and !reindexAction.canceled and !reindexAction.error}">
              <p id="completed" class="txt--meta">
                <h:outputText
                  value="#{msgs.format('jsf.ManageSearch.Completed', reindexAction.elapsedTime)}" />
              </p>
            </ui:fragment>
            <ui:fragment rendered="#{reindexAction.canceled}">
              <p id="aborted" class="txt--meta">
                <h:outputText escape="false"
                  value="#{msgs.format('jsf.ManageSearch.Aborted', reindexAction.elapsedTime)}" />
              </p>
            </ui:fragment>
            <ui:fragment rendered="#{reindexAction.error}">
              <p class="txt--meta txt--danger">
                <h:outputText escape="false"
                  value="#{msgs['jsf.manageSearch.ErrorMessage']}" />
                <br/>
                <h:outputText escape="false"
                  value="#{msgs['jsf.manageSearch.PleaseReindex']}" />
              </p>
            </ui:fragment>

            <a4j:outputPanel layout="block">
              <a4j:poll render="progress,actions" enabled="#{reindexAction.inProgress}"/>

              <ui:fragment rendered="#{reindexAction.inProgress}">
                <ul class="list--panel bg--higher">
                  <li>
                    <div class="list__item__content">
                      <div class="list__item__info">
                        <span class="list__title">
                          #{msgs.format('jsf.manageSearch.ProgressMessage', reindexAction.reindexProgress, reindexAction.reindexCount)}
                        </span>
                        <span class="list__item__meta">
                          <span title="#{msgs['jsf.ManageSearch.ElapsedTime']}">
                            <i class="i i--clock"></i> #{reindexAction.elapsedTime}
                          </span>
                          <br/>
                          <h:outputText
                            value="#{msgs.format('jsf.ManageSearch.RemainingTime', reindexAction.estimatedTimeRemaining)}" />
                        </span>
                      </div>
                      <div class="list__item__actions">
                        <zanata:loader layout="inline"/>
                        <span class="txt--meta">#{reindexAction.progressPercentage}%</span>
                      </div>
                    </div>
                    <div class="progress-bar">
                      <span class="progress-bar__item"></span>
                      <span class="progress-bar__success" style="width:#{reindexAction.progressPercentage}%"></span>
                    </div>
                  </li>
                </ul>
              </ui:fragment>
            </a4j:outputPanel>
          </div>
        </h:panelGroup>
      </div>

      <div class="g__item w--1-m w--5-8-l w--2-3">
        <h:form id="form">
            <h:panelGroup layout="block"  id="actions">
                <table class="bg--pop-highest tableSearch l--push-bottom-1">
                  <thead>
                    <tr>
                      <th class="l--pad-v-half">
                        <h:selectBooleanCheckbox value="#{reindexAction.selectAll}" id="selectAllCheck"
                          disabled="#{not reindexAction.canceled and reindexAction.inProgress}">
                          <a4j:ajax event="click" render="actions" execute="@this"/>
                        </h:selectBooleanCheckbox>
                        <span class="l--push-left-quarter">#{msgs['jsf.manageSearch.Table']}</span>
                      </th>
                      <th class="l--pad-v-half">
                        <h:selectBooleanCheckbox value="#{reindexAction.purgeAll}"
                          disabled="#{not reindexAction.canceled and reindexAction.inProgress}">
                          <a4j:ajax event="click" render="actions" execute="@this"/>
                        </h:selectBooleanCheckbox>
                        <span class="l--push-left-quarter">
                          #{msgs['jsf.manageSearch.purge']} <i class="i i--info txt--meta" title="#{msgs['jsf.manageSearch.purge.Description']}"></i>
                        </span>
                      </th>
                      <th class="l--pad-v-half">
                        <h:selectBooleanCheckbox value="#{reindexAction.reindexAll}" id="reindexAllChk"
                          disabled="#{not reindexAction.canceled and reindexAction.inProgress}">
                          <a4j:ajax event="click" render="actions" execute="@this"/>
                        </h:selectBooleanCheckbox>
                        <span class="l--push-left-quarter">
                          #{msgs['jsf.manageSearch.reindex']} <i class="i i--info txt--meta" title="#{msgs['jsf.manageSearch.reindex.Description']} #{msgs['jsf.manageSearch.reindex.Description.details']}"></i>
                        </span>
                      </th>
                      <th class="l--pad-v-half">
                        <h:selectBooleanCheckbox value="#{reindexAction.optimizeAll}"
                          disabled="#{not reindexAction.canceled and reindexAction.inProgress}">
                          <a4j:ajax event="click" render="actions" execute="@this"/>
                        </h:selectBooleanCheckbox>
                        <span class="l--push-left-quarter">
                          #{msgs['jsf.manageSearch.optimize']} <i class="i i--info txt--meta" title="#{msgs['jsf.manageSearch.optimize.Description']} #{msgs['jsf.manageSearch.optimize.TempFileWarning']}"></i>
                        </span>
                      </th>
                    </tr>
                  </thead>
                  <tbody class="bg--high">
                    <ui:repeat value="#{reindexAction.classes.toArray()}" var="clazz">
                      <tr class="bg--higher--hover">
                        <td class="l--pad-v-half">
                          <h:selectBooleanCheckbox value="#{clazz.selectAll}"
                            disabled="#{not reindexAction.canceled and reindexAction.inProgress}">
                            <a4j:ajax event="click" render="actions" execute="@this"/>
                          </h:selectBooleanCheckbox>
                          <span class="l--push-left-quarter"><h:outputText value="#{clazz.className}"/></span>
                        </td>
                        <td class="l--pad-v-half">
                          <h:selectBooleanCheckbox value="#{clazz.purge}" execute="@this"
                            disabled="#{not reindexAction.canceled and reindexAction.inProgress}">
                            <a4j:ajax event="click" render="actions" execute="@this"/>
                          </h:selectBooleanCheckbox>
                        </td>
                        <td class="l--pad-v-half">
                          <h:selectBooleanCheckbox value="#{clazz.reindex}"
                            disabled="#{not reindexAction.canceled and reindexAction.inProgress}">
                            <a4j:ajax event="click" render="actions" execute="@this"/>
                          </h:selectBooleanCheckbox>
                        </td>
                        <td class="l--pad-v-half">
                          <h:selectBooleanCheckbox value="#{clazz.optimize}"
                            disabled="#{not reindexAction.canceled and reindexAction.inProgress}">
                            <a4j:ajax event="click" render="actions" execute="@this"/>
                          </h:selectBooleanCheckbox>
                        </td>
                      </tr>
                    </ui:repeat>
                  </tbody>
                </table>

                <ui:fragment rendered="#{not reindexAction.isAnyOptionSelected() or (not reindexAction.canceled and reindexAction.inProgress)}">
                  <button id="clearSelection" onclick="return false;"
                    class="button--link l--push-right-half" disabled="disabled">
                    #{msgs['jsf.ManageSearch.SelectNone']}
                  </button>

                  <button id="reindex" onclick="return false;"
                    class="button button--primary" disabled="disabled">
                    #{msgs['jsf.ManageSearch.PerformSelectedActions']}
                  </button>
                </ui:fragment>

                <ui:fragment rendered="#{reindexAction.isAnyOptionSelected() and (reindexAction.canceled or not reindexAction.inProgress)}">
                  <button id="clearSelection" onclick="clearAllSelection();return false;"
                    class="button--link l--push-right-half">
                    #{msgs['jsf.ManageSearch.SelectNone']}
                  </button>

                  <button id="reindex" onclick="executeSelection();return false;"
                    class="button button--primary ">
                    #{msgs['jsf.ManageSearch.PerformSelectedActions']}
                  </button>
                </ui:fragment>

              </h:panelGroup>
        </h:form>
      </div>
    </div>
  </ui:define>

</ui:composition>
